package com.project.comom;

import com.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/commonFile")
@Api(tags = "通用文件接口")
public class CommonFileController {

    @Autowired
    private CommonFileService fileService;

    /**
     * 传文件 -单个
     *
     * @param file
     * @param modelName
     * @param businessId
     * @return 文件id
     */
    @ApiOperation(value = "上传文件 -单个")
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(MultipartFile file,
                                 @ApiParam(value = "所属模块 如:zXContract,projectApplyInfo等,一个模块写统一的名称", required = true)
                                 @RequestParam(value = "modelName", required = true) String modelName,
                                 @ApiParam(value = "正在操作表的主键id,可以为空", required = false)
                                 @RequestParam(value = "businessId", required = false) String businessId) throws Exception {
        String id = fileService.uploadFile(file, modelName, businessId);
        return AjaxResult.success("上传成功", id);
    }


    /**
     * 上传文件 - 多个
     *
     * @param files
     * @param modelName
     * @param businessId
     * @return 文件名和文件id的map集合
     */
    @ApiOperation(value = "上传文件 -多个")
    @PostMapping("/uploadFiles")
    public AjaxResult uploadFiles(List<MultipartFile> files,
                                  @ApiParam(value = "所属模块 如:zXContract,projectApplyInfo等,一个模块写统一的名称", required = true)
                                  @RequestParam(value = "modelName", required = true) String modelName,
                                  @ApiParam(value = "正在操作表的主键id,可以为空", required = false)
                                  @RequestParam(value = "businessId", required = false) String businessId
    ) throws Exception {
        List list = fileService.uploadFiles(files, modelName, businessId);
        return AjaxResult.success(list);
    }


    /**
     * 下载文件
     *
     * @param fileId   文件id
     * @param response
     * @throws Exception
     */
    @ApiOperation(value = "下载文件")
    @GetMapping("/download")
    public void download(@ApiParam(value = "文件id", required = true)
                         @RequestParam("fileId") String fileId, HttpServletResponse response) throws Exception {
        fileService.download(fileId, response);
    }

    /**
     * 删除文件
     *
     * @param fileId 文件id
     * @throws Exception
     */
    @ApiOperation(value = "删除文件")
    @GetMapping("/deleteFile")
    public AjaxResult deleteFile(
            @ApiParam(value = "文件id", required = true)
            @RequestParam("fileId") String fileId) {
        Boolean bl = fileService.delFile(fileId);
        return bl ? AjaxResult.success() : AjaxResult.error();
    }


    /**
     * 填充文件表objectId
     *
     * @param fileId 文件id
     * @throws Exception
     */
    @ApiOperation(value = "填充文件表objectId,关联主表")
    @GetMapping("/fillObjectId")
    public AjaxResult fillObjectId(
            @ApiParam(value = "文件id", required = true)
            @RequestParam("fileId") String fileId,
            @ApiParam(value = "业务主表id", required = true)
            @RequestParam("businessId") String businessId) {
        Boolean bl = fileService.fillObjectId(fileId, businessId);
        return bl ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 获取导出可选属性
     *
     * @param exportType 业务名称
     * @param proClass   项目类别
     * @return
     */
    @ApiOperation(value = "获取导出可选属性：")
    @GetMapping("/getExportStr")

    public AjaxResult getExportStr(@ApiParam(value = "导出业务：init,alteration,midleCheck,complete,ethical,resServ",required = true) String exportType,
                                   @ApiParam(value = "项目类别：xJProject zXProject yfContract") String proClass){
        return AjaxResult.success(fileService.getExportStr(exportType,proClass));
    }
}
